﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using CompressDataSet;
using System.IO.Compression;
using System.Runtime.Serialization.Formatters.Binary;
using System.Diagnostics;
using System.Threading;

namespace LearnDrive
{
    public partial class frmMain : Form
    {
        TimeSpan regtime;
        WebReference2.Heart s = new WebReference2.Heart();
        private string _login_name;
        private string _login_pass;
        private int typekm = 1;//科目
        private int typelx = 1;//类型
        private bool isstop = false;
        private int hearnum = 0;
        private bool isupdate = true;
        public frmMain(string name, string pass)
        {
            this._login_name = name;
            this._login_pass = pass;

            Control.CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();          
            
        }
        public static void InitMenu(ToolStripMenuItem tsMi, EventHandler e)
        {
            DirectoryInfo dirinfo = new DirectoryInfo(Application.StartupPath + @"\skins");
            string str, filename;
            ToolStripMenuItem temp = new ToolStripMenuItem();
            temp.Text = "默认";
            temp.Tag = "no";
            temp.Click += new EventHandler(e);
            tsMi.DropDownItems.Add(temp);
            FileInfo[] allfile = null;
            if (Directory.Exists(Application.StartupPath + @"\skins"))
                allfile = dirinfo.GetFiles("*.ssk");
            if (allfile != null)
                foreach (FileInfo fi in allfile)
                {
                    temp = new ToolStripMenuItem();
                    str = fi.Name;
                    filename = str.Substring(0, str.IndexOf('.'));
                    temp.Text = filename;
                    temp.Tag = fi.FullName;
                    temp.Click += new EventHandler(e);
                    tsMi.DropDownItems.Add(temp);
                }
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            this.Text = "大拇指驾考交规速成-" + Myinfo.version;
            regtime = Myinfo.softtime;
            initPic();

        }        
        

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (isupdate)
            {
                isupdate = false;
                frmUpdateDB f = new frmUpdateDB();
                f.Show();
            }
            if (regtime.TotalSeconds > 0)
            {
                if (regtime.Days < 1)
                    this.tsslTime.ForeColor = Color.Red;
                this.tsslTime.Text = "剩余时间：" + AShelp.DateDiff(ref regtime);
            }
            else
            {
                if (!isstop)
                {
                    isstop = true;
                    timer1.Stop();                    
                    frmMsg f = new frmMsg("软件已到期,请联系软件公司！", 5, true);
                    f.ShowDialog();
                    Application.ExitThread();
                }
            }
        }
        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("您真的要退出吗？", "提示", MessageBoxButtons.YesNo) == DialogResult.No)
            {
                e.Cancel = true;
            }

        }

        private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            //Application.ExitThread();

            string key = "AutoSend";
            string iv = "weiwei@1";

            string strPass = this._login_name + "|" + this._login_pass + "|1|" + DateTime.Now;

            string md5strPass = Tools.Encode(strPass, key, iv);

            WebReference2.Heart hwm = new WebReference2.Heart();


            //result:0|时间，代表退出失败，1|时间，代表退出成功
            try
            {
                string result = hwm.SetIsUse(md5strPass);

                string re = Tools.Decode(result, key, iv);
            }
            catch (Exception ex)
            {
                if (ex.ToString().Contains("无法连接到远程服务器"))
                {
                    MessageBox.Show("无法连接到远程服务器,程序将退出！");
                }

            }
            finally
            {

                Application.ExitThread();
            }
        }

        private void radioButton1_Click(object sender, EventArgs e)
        {
            RadioButton r = sender as RadioButton;
            if (r != null)
            {
                int.TryParse(r.Tag.ToString(), out typekm);
                if (typekm == 1)
                    MySelect.typekm = "科目一";
                else if (typekm == 4)
                    MySelect.typekm = "科目四";
                else if (typekm == 5)
                    MySelect.typekm = "恢复驾照";
            }
        }

        private void gettitle(object sender)
        {
            //Button b = sender as Button;
            //if (b != null)
            //    groupBox2.Text = "选择－" + b.Text;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            gettitle(sender);
            typelx = 1;
            MySelect.typelx = "C1C2C3C4";
            showfrm();
        }

        private void showfrm()
        {
            frmPro f = new frmPro(typekm, typelx);
            f.ShowDialog();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            gettitle(sender);
            typelx = 2;
            MySelect.typelx = "A1A3B1";
            showfrm();

        }

        private void button3_Click(object sender, EventArgs e)
        {
            gettitle(sender);
            typelx = 3;
            MySelect.typelx = "A2B2";
            showfrm();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            gettitle(sender);
            typelx = 4;
            MySelect.typelx = "DEF";
            showfrm();
        }

        //心跳
        private void timer_heart_Tick(object sender, EventArgs e)
        {
            string key = "AutoSend";
            string iv = "weiwei@1";

            string strPass = this._login_name + "|" + this._login_pass + "|1|" + DateTime.Now;

            string md5strPass = Tools.Encode(strPass, key, iv);

            WebReference2.Heart hwm = new WebReference2.Heart();

            try
            {
                //0、账号正常，1、账号被冻结，-1、用户名密码不正确
                string re = hwm.SetHeart(md5strPass);

                if (re == "0")
                {
                    hearnum = 0;
                }
                else if (re == "1")
                {
                    timer_heart.Stop();
                    MessageBox.Show("账号被冻结！");
                    Application.ExitThread();
                }
                else if (re == "-1")
                {
                    timer_heart.Stop();
                    MessageBox.Show("用户名密码不正确，请重新登录！");
                    Application.ExitThread();
                }
                else
                {
                    timer_heart.Stop();
                    MessageBox.Show("程序出错！");
                    Application.ExitThread();
                }
            }
            catch (Exception ex)
            {
                string str = "";
                string strDateInfo = "连接网络异常：" + DateTime.Now.ToString() + "\r\n";
                Exception error =ex;
                if (error != null)
                {
                    str = string.Format(strDateInfo + "异常类型：{0}\r\n异常消息：{1}\r\n异常信息：{2}\r\n",
                                        error.GetType().Name, error.Message, error.StackTrace);
                }
                else
                {
                    str = string.Format("应用程序线程错误:{0}", e);
                }
                AShelp.writeLog(str);
                hearnum++;
                if (hearnum >1)
                {
                    timer_heart.Stop();
                    MessageBox.Show("无法连接到远程服务器,程序将自动退出！");
                    Application.ExitThread();
                }
                else if (hearnum >0)
                {
                    MessageBox.Show("无法连接到远程服务器,程序将在5分钟后自动退出,请及时连网！");
                }
            }
        }
        private void initPic()
        {
            string path = Application.StartupPath + "\\sysvoice\\";
            pictureBox6.Image = Image.FromFile(path+"1.png");
            pictureBox7.Image = Image.FromFile(path + "4.png");
            pictureBox8.Image = Image.FromFile(path + "5.png");
        }
        private void pictureBox6_Click(object sender, EventArgs e)
        {
            string path = Application.StartupPath + "\\sysvoice\\";
            PictureBox r=sender as PictureBox;
            if (r != null)
            {
                int.TryParse(r.Tag.ToString(), out typekm);
                if (typekm == 1)
                    MySelect.typekm = "科目一";
                else if (typekm == 4)
                    MySelect.typekm = "科目四";
                else if (typekm == 5)
                    MySelect.typekm = "恢复驾照";
                initPic();
                r.Image = Image.FromFile(path + typekm + "r.png");
            }

        }

    }
}
